Mean Example

Figure: Mean Function
\begin{figure}
\begin{verbatim}
//---------------------------------------------...
... 
 { 
 m = size (x)[2];
 }

 return sum( x ) / m;
};\end{verbatim}

\end{figure}
The rfile mean.r (See Figure [*]) has several noteworthy attributes.
  1. The top lines of the file contain comments which document the usage of the function. This is useful, since the the rlab help function will copy the contents of each rfile to the screen. If documentation comments are include in the topmost portion of the file, then users will have convenient access to the help comments.
  2. mean does not do any error checking on the input arguments. Whether it should or not is debatable. We will present arguments for and against:
    For:
    Calling mean with a string, or a list variable as argument will result in a slightly obscure error message.
    > mean ( "string constant" )
    rlab: NULL, invalid type for sum()
    near line 25, file: /usr/local/lib/rlab/rlib/mean.r
    > mean ( << [1,2,3]; 100 >> )
    rlab: NULL, invalid type for sum()
    near line 25, file: /usr/local/lib/rlab/rlib/mean.r
    >
    
    Instead of mean reporting the error, the error is propagated down to sum. This is somewhat confusing, since the user committed the error with the function mean.
    Against:
    The function is obviously intended to calculate the mean value of a vector, or matrix object. And, for any numeric argument mean will work just fine. Users who are foolish enough to try and compute the mean value of a string, or a heterogeneous object (a list) should not be catered to. That is, users who use the function correctly, should not have to pay a performance penalty.